package com.example.leetdemo.process;

import java.util.*;

public class Solution2368 {

    public int reachableNodes(int n, int[][] edges, int[] restricted) {
        Set<Integer> set = new HashSet<>();
        Map<Integer, Set<Integer>> map = new HashMap<>();
        Set<Integer> errorSet = new HashSet<>();
        for (int i = 0; i < restricted.length; i++) {
            errorSet.add(restricted[i]);
        }
        for (int i = 0; i < edges.length; i++) {
            if (errorSet.contains(edges[i][0]) || errorSet.contains(edges[i][1])) {
                continue;
            }
            Set<Integer> temp1 = map.getOrDefault(edges[i][0], new HashSet<>());
            temp1.add(edges[i][1]);
            map.put(edges[i][0], temp1);

            Set<Integer> temp2 = map.getOrDefault(edges[i][1], new HashSet<>());
            temp2.add(edges[i][0]);
            map.put(edges[i][1], temp2);
        }
        set.add(0);
        Set<Integer> temp = map.getOrDefault(0, new HashSet<>());
        while (temp.size() > 0) {
            Set<Integer> tempSet = new HashSet<>();
            for (Integer i : temp) {
                if (!set.contains(i)) {
                    set.add(i);
                    tempSet.add(i);
                }
            }
            temp = tempSet;
        }
        return set.size();
    }

}
